package com.filter;

import com.alibaba.fastjson.JSON;
import com.entity.User;
import sun.security.provider.MD5;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class loginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        /*验证此用户是否已经登录过了*/

        HttpServletRequest request =(HttpServletRequest)servletRequest;

        //http://localhost:8080/webJdbc/ModifyNews.jsp
        String url=request.getRequestURL().substring(0,1);


        HttpSession session=request.getSession();
        HttpServletResponse response =(HttpServletResponse)servletResponse;
         Object obj =session.getAttribute("user");

         User user=null;
         if(obj !=null){
              user =(User)obj;
         }
         if(user !=null){
             filterChain.doFilter(servletRequest,servletResponse);
         }else {
             request.getRequestDispatcher("login.jsp").forward(request,response);
         }
    }

    @Override
    public void destroy() {

    }
}
